package com.booth.apply.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booth.apply.entity.Apply;
import com.booth.apply.entity.dto.ApplyGetDto;
import com.booth.apply.entity.dto.OptionApplyDto;
import com.booth.apply.service.IApplyService;
import com.booth.apply.utils.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/apply/apply")
public class ApplyController {

    @Autowired
    private IApplyService iApplyService;

    @ApiOperation("查询申请数据")
    @GetMapping
    public CommonResult getAllAppInfo(ApplyGetDto applyGetDto) {
        String DESC = "desc";
        LambdaQueryWrapper<Apply> queryWrapper = new QueryWrapper<Apply>().lambda()
                .eq(applyGetDto.getFlag() != null, Apply::getFlag, applyGetDto.getFlag())
                .eq(applyGetDto.getUserAccount() != null, Apply::getUserAccount, applyGetDto.getUserAccount())
                .eq(applyGetDto.getIdNumber() != null, Apply::getIdNumber, applyGetDto.getIdNumber())
                .eq(applyGetDto.getAdminJobNumber() != null, Apply::getAdminJobNumber, applyGetDto.getAdminJobNumber())
                .like(!StringUtils.isEmpty(applyGetDto.getRealName()), Apply::getRealName, applyGetDto.getRealName());

        if (applyGetDto.getCreateTimeOrderly() != null) {
            if (applyGetDto.getCreateTimeOrderly().equals(DESC)) {
                queryWrapper.orderByDesc(Apply::getApplyTime);
            } else {
                queryWrapper.orderByAsc(Apply::getApplyTime);
            }
        }
        return CommonResult.success(iApplyService.page(new Page<>(applyGetDto.getCurrentPage(), applyGetDto.getPageSize()), queryWrapper));
    }


    @ApiOperation("同意申请")
    @PostMapping("/agree")
    public CommonResult agreeApply(@RequestBody OptionApplyDto optionApplyDto) {
        return iApplyService.agreeApply(optionApplyDto);
    }

    @ApiOperation("拒绝申请")
    @PostMapping("/refuse")
    public CommonResult refuseApply(@RequestBody OptionApplyDto optionApplyDto) {
        return iApplyService.refuseApply(optionApplyDto);
    }


}

